<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 11/11/14
 * Time: 10:45 AM
 */

class Lesson extends Eloquent
{
    use \Illuminate\Database\Eloquent\SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }


    public function tone()
    {
        return $this->belongsTo('Tone', 'tone_id');
    }

    public function category()
    {
        return $this->belongsTo('Category', 'category_id');
    }

    public function topics()
    {
        return $this->belongsToMany('Topic', 'topicables', 'lesson_id', 'topic_id');
    }

    public function tags()
    {
        return $this->belongsToMany('Tag', 'taggable', 'lesson_id', 'tag_id');
    }

    public function comments()
    {
        return $this->hasMany('Comment', 'lesson_id');
    }

    public function favourites()
    {
        return $this->hasMany('Favourite', 'lesson_id');
    }

    public function likes()
    {
        return $this->hasMany('Like', 'lesson_id');
    }

    public function thanks()
    {
        return $this->hasMany('Thank', 'lesson_id');
    }

    public function thumbnail_link()
    {
        if (!empty($this->thumbnail))
            return route('show-image', $this->thumbnail);
        else
        {
            $headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $this->youtube);
            if (!strpos($headers[0], '200')) {
                return '';
            }
        }
        return Config::get('constants.HOST_IMG_YOUTUBE') . $this->youtube . '/mqdefault.jpg';
    }

    public function generate_link()
    {
        return route('bai-giang-chi-tiet', array('id' => $this->id, 'slug' => $this->slug));
    }
} 